<?php
namespace Api\Model;
use Think\Model;

class merchantModel extends Model{
    public function datacheck(){
        // 检查必填参数合法性
        $RequiredParam = array('partner' , 'ordernumber' , 'banktype' , 'paymoney' , 'order_time', 'ClientIp' , 'notify_url' , 'sign');
        for ($i = 0; $i<count($RequiredParam) ; $i++){
            if(empty($_REQUEST[$RequiredParam[$i]])){
                return $RequiredParam[$i];
            }
        }

        if(strlen($_REQUEST['ordernumber']) > 32) return 'ordernumber';
        if(($_REQUEST['banktype'] == 'weixinGzh_pay' || $_REQUEST['banktype'] == 'bank_pay') && intval($_REQUEST['user_id']) == 0) return 'user_id';
        if (!is_numeric($_REQUEST['paymoney']))  return 'paymoney';
        if(intval($_REQUEST['order_time']) < 10) return 'order_time';
            if(strlen($_REQUEST['sign']) != 32) return 'sign';
        if(!merchantModel::checkIP($_REQUEST['ClientIp'])) return 'ClientIp';
        return '0';
    }

    public static function checkIP($ip){
        $ip = trim($ip);
        if(strpos($ip, '/') !== false)
        {
            $s = explode('/', $ip);
            preg_match('/^(((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)(\.((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)){3})$/', $s[0], $matches);
            if(!empty($matches) and $s[1] > 0 and $s[1] < 36) return true;
        }
        else
        {
            preg_match('/^(((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)(\.((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d)){3})$/', $ip, $matches);
            if(!empty($matches)) return true;
        }
        return false;
    }
}